const express=require('express');
const app=express();
const md5=require('md5');
const ejs=require('ejs');
const mdb=require('./module/mongodb.js');
const session=require('express-session');
// 处理ObjectId数据
const ObjectId=require('mongodb').ObjectID;
//配置socket.io
const http=require('http').Server(app);
const io=require('socket.io')(http);

// session默认存储在服务器上，如果不做特殊处理，关闭服务器，session消失.可以配置session持久化
const NedbStore=require('nedb-session-store')(session);
// 配置可以使用session
app.use(session({
	secret: 'keyboard cat',
	resave: false,
	saveUninitialized: true,
	cookie:{
		// 设置时长ms
		maxAge:1000*60*60*24*7
	},
	// 配置持久化
	store:new NedbStore({
		// 存储的session地址 名字可以自定义
		filename:'user.db'
	})
}))

//设置ejs模板引擎
app.set('view engine','ejs');

//呈递静态资源
app.use(express.static('./views'))
app.use(express.static('./public'))
//呈递ejs页面，res.render('文件名')   自动去views目录寻找该文件
app.get('/log',(req,res)=>{
	let obj=req.session.user;
	console.log(obj==undefined)
	obj==undefined||obj==""?obj={name:false,pwd:false}:obj
	console.log(obj,'加载')
	mdb.findAll('mydb','stu',obj,(r)=>{
		res.render('log',{user:0,obj:obj})
	})
})
app.get('/reg',(req,res)=>{
	res.render('reg')
})
//主页面
app.get('/',(req,res)=>{
	var sessionuser=req.session.user;
	var obj=req.query;
	var num=parseInt(req.query.num);
	console.log(obj,obj.name|0,"index")
	
	mdb.findAll('mydb','blog',{},(r)=>{
		console.log(r.length)
		res.render('index',{user:sessionuser.name|0,r:r,flag:1})
	},{},num*10,10)
})
//博客页面，更多刷新
.get('/page',(req,res)=>{
	console.log("page")
	let num=req.query.num;
	var sessionuser=req.session.user;
	mdb.findAll('mydb','blog',{},(r)=>{
		console.log(r.length,'page')
		mdb.findAll('mydb','blog',{},(r)=>{
			console.log(r.length)
			res.send({user:sessionuser.name|0,r:r,flag:0})
		},{},num*10,10)
	})
})
//显示当前用户的博客
.get('/pagelist',(req,res)=>{
	console.log("pagelist")
	let obj=req.query;
	mdb.findAll('mydb','blog',obj,(r)=>{
		console.log(r.length,'page')
		res.send(r)
	})
})
//点赞刷新
.get('/pagesup',(req,res)=>{
	var obj=req.query
	console.log(obj,"pagesup")
	mdb.findAll('mydb','blog',{},(r)=>{
		console.log(r,'page')
		res.send(r)
	})
})
//登录操作
app.get('/login',(req,res)=>{
	var obj={
		name:req.query.name,
		pwd:md5(req.query.pwd)
	};
	console.log(obj,'登录')
	mdb.findAll('mydb','stu',obj,(r)=>{
		if(r!=""){
			req.session.user=obj;
		}
		console.log(r)
		res.json([r,obj])
	})
})
//注册操作
app.get('/regin',(req,res)=>{
	var obj={
		name:req.query.name,
		pwd:md5(req.query.pwd)
	};
	console.log(obj)
	var objfind={
		name:req.query.nam,
		pwd:md5(req.query.pwd)
	};
	mdb.findAll('mydb','stu',objfind,(r)=>{
		console.log(r)
		if(r==""){
			mdb.add('mydb','stu',obj,(r)=>{
				res.json(r)
			})
		}else{
			res.send("名字已被使用")
		}
		
	})
	
})
//退出账号
app.get('/bowOut',(req,res)=>{
	req.session.user="";
	console.log(req.query)
	res.render('index',{user:0,r:{},flag:0})
})
//注销
app.get('/logOut',(req,res)=>{
	console.log(req.query)
	var obj={
		name:req.query.name
	};
	mdb.remove('mydb','stu',obj,(r)=>{
		res.render('index',{user:0,r:{},flag:0})
	})
})

//发微博
app.get('/sendblog',(req,res)=>{
	var obj={
		user:req.query.user,
		title:req.query.title,
		msg:req.query.msg,
		support:[],
		discuss:[]
	};
	console.log(obj)
	if(obj.user){
		mdb.add('mydb','blog',obj,(r)=>{
			res.render('index',{user:obj.user|0,r:r,flag:0})
		})
	}else{
		res.send("错误操作")
	}
})
//点赞
app.get('/sendsupport',(req,res)=>{
	var _id=new ObjectId(req.query._id);
	var oldobj={
		_id: _id
	};
	console.log(oldobj,req.query)
	if(req.query.user==0){
		res.send('您没有登陆');
		return;
	}
	mdb.findAll('mydb','blog',oldobj,(sup)=>{
		console.log(sup,sup[0].support,req.query.user,'点赞')
		// console.log(sup.support.indexOf(req.query.user))
		if(sup[0].support.indexOf(req.query.user)===-1){
			sup[0].support.splice(0,0,req.query.user)
			var obj={$set:{"support":sup[0].support}};
			console.log(sup[0].support,'点赞')
			mdb.change('mydb','blog',oldobj,obj,(r)=>{
				console.log(r.result)
				res.send({r:r,flag:true})
			})
			return
		}
		let num=sup[0].support.indexOf(req.query.user)
		sup[0].support.splice(num,1)
		var obj={$set:{"support":sup[0].support}};
		console.log(sup[0].support,'点赞')
		mdb.change('mydb','blog',oldobj,obj,(r)=>{
			console.log(r.result)
			res.send({r:r,flag:false})
		})
	})
})

//评论
app.get('/senddiscuss',(req,res)=>{
	var _id=new ObjectId(req.query._id);
	var oldobj={
		_id: _id
	};
	
	console.log(oldobj,req.query,req.query.user==0,req.query.user==undefined)
	if(req.query.user==0||req.query.user==undefined){
		res.send('您没有登陆')
	}else{
		mdb.findAll('mydb','blog',oldobj,(r)=>{
			r[0].discuss.push({name:req.query.user,msg:req.query.discuss})
			console.log("发评论",r[0].discuss)
			var obj={$set:{"discuss":r[0].discuss}};
			/*for(let i in r[0].discuss){
				console.log(r[0].discuss[i])
			}*/
			mdb.change('mydb','blog',oldobj,obj,(r)=>{
				console.log(r.result)
				// res.render('index',{user:obj.user|0,r:r,flag:0})
				res.send({user:obj.user,r:r,flag:0})
			})
		})
		
	}
})
//查找微博
app.get('/findblog',(req,res)=>{
	console.log(req.query)
	let obj={
		msg:/[req.query.msg]/
	}
	mdb.findAll('mydb','blog',obj,(r)=>{
		console.log(r)
		// res.render('index',{user:obj.user,r:r,flag:0})
		res.send({user:obj.user,r:r,flag:0})
	})
})
//聊天室
app.get('/chat',(req,res)=>{
	let obj=req.query;
	console.log(obj)
	res.render('chat',{user:obj.user})
})
.get('/msg',(req,res)=>{
	console.log(req.query,"msg")
	
})
//配置长连接
io.on('connection',(socket)=>{
	//接收客户端数据  on监听数据接收
	socket.on('chat',(msg)=>{
		//发送数据给客户端
		io.emit('send',msg)
	})
})

app.get('/getAll',(req,res)=>{
	mdb.findAll('mydb','blog',{},(r)=>{
		res.send(r)
	})
})
.get("/user",(req,res)=>{
	mdb.findAll('mydb','stu',{},(r)=>{
		res.send(r)
	})
})
.get("/ses",(req,res)=>{
	if(req.session.user){
		console.log(req.session.user)
	}else{
		res.send('已清除')
	}
})


http.listen(8989)